<?php

declare(strict_types=1);

namespace App\Common;

use Gelf\Publisher;
use Gelf\Transport\AbstractTransport;
use Monolog\Handler\GelfHandler as MonologGelfHandler;
use Monolog\Logger;

class GelfHandler extends MonologGelfHandler
{
    public function __construct($host, $port, $transportType, $level = 'debug', $path = null)
    {
        $transport = $this->getTransport($transportType, $host, (int) $port, $path);

        parent::__construct(new Publisher($transport), Logger::toMonologLevel($level));
    }

    protected function getTransport(string $transport, string $host, int $port, ?string $path = null): AbstractTransport
    {
        switch (strtolower($transport)) {
            case 'tcp':
                // TODO
//                return new TcpTransport($host, $port);

            case 'http':
                // TODO
//                return new HttpTransport($host, $port, $path ?? HttpTransport::DEFAULT_PATH);

            default:
                return new UdpTransport($host, $port);
        }
    }

    protected function write($record): void
    {
        parent::write($record);
    }

    public function close(): void
    {
        parent::close();
    }
}
